<?php

class SiteView extends dmq\View
{
	public function buildWebsite( $site, $section )
	{
		$css = array_unique( array_merge( $site->css(), $section->css() ) );
		$js = array_unique( array_merge( $site->js(), $section->js() ) );

		$this->markup( '<!doctype html>' . "\r\n" );
		$this->markup( '<html>' . "\r\n" );
		$this->markup( '  <head>' . "\r\n" );
		$this->markup( '    <title>' . $site->title() . " &ndash; " . $section->title() . '</title>' . "\r\n" );
		
		// Add external CSS.
		foreach ( $css as $fileName )
		{
			$this->markup( '    <link rel="stylesheet" href="css/' . $fileName . '.css" type="text/css" />' . "\r\n" );
		}
		
		// Add external JS.
		foreach ( $js as $fileName )
		{
			$this->markup( '    <script type="text/javascript" src="js/' . $fileName . '.js"></script>' . "\r\n" );
		}
		
		if ( strlen( $site->ijs() . $section->ijs() ) > 0 )
		{
			$this->markup( '<script type="text/javascript">' . "\r\n" );
			$this->markup( $site->ijs() . "\r\n" );
			$this->markup( $section->ijs() . "\r\n" );
			$this->markup( '</script>' . "\r\n" );
		}

		$this->markup( '  </head>' . "\r\n" );
		$this->markup( '  <body>' . "\r\n" );
		$this->markup( '    <div id="page-wrapper">' . "\r\n" );
		$this->markup( '      <div id="header">' . "\r\n" );
		$this->markup( '        <h1><a href="index.php">Irinori</a></h1>' . "\r\n" );
		$this->markup( '        <ul id="main-menu">' . "\r\n" );
		
		$this->markup( '          <li' . ( ( $section->name() == "Lobby" ) ? ' class="active"' : '' ). '><a href="index.php">Lobby</a></li>' . "\r\n" );
		
		if ( $this->auth()->isLoggedIn() )
		{
			$this->markup( '          <li' . ( ( $section->name() == "Login" ) ? ' class="active"' : '' ). '><a href="index.php?c=login&amp;a=logout">Logout "' . $this->auth()->user()->name() . '"</a></li>' . "\r\n" );
		}
		else
		{
			$this->markup( '          <li' . ( ( $section->name() == "Login" ) ? ' class="active"' : '' ). '><a href="index.php?c=login">Login</a></li>' . "\r\n" );
			$this->markup( '          <li' . ( ( $section->name() == "Registration" ) ? ' class="active"' : '' ). '><a href="index.php?c=registration">Sign Up</a></li>' . "\r\n" );
		}

		$this->markup( '        </ul>' . "\r\n" );
		$this->markup( '      </div>' . "\r\n" );
		$this->markup( '      <div id="content-wrapper">' . "\r\n" );
		$this->markup( '<h2>' . $section->title() . '</h2>' . "\r\n" );
		
		$this->markup( $section->output() );
		
		$this->markup( '      </div>' . "\r\n" );
		$this->markup( '    </div>' . "\r\n" );
		$this->markup( '  </body>' . "\r\n" );
		$this->markup( '</html>' . "\r\n" );
	}
}
